/****************************************************
文件：SceneComponent.cs
作者：UnitySir
邮箱：zouajck@foxmail.com
日期：2021/03/22 20:24:36
功能：Nothing
*****************************************************/

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;

namespace DSFramework
{
    public class SceneComponent : DSComponent
    {
        public override void InitCmpts()
        {
        }

        /// <summary>
        /// 切换场景 同步
        /// </summary>
        /// <param name="name"></param>
        public void LoadScene(string name, UnityAction fun)
        {
            //场景同步加载
            SceneManager.LoadScene(name);
            //加载完成过后 才会去执行fun
            fun();
        }

        /// <summary>
        /// 提供给外部的 异步加载的接口方法
        /// </summary>
        /// <param name="name"></param>
        /// <param name="fun"></param>
        public void LoadSceneAsyn(string name, Action fun)
        {
            DSEntity.Mono.StartCoroutine(ReallyLoadSceneAsyn(name, fun));
        }

        /// <summary>
        /// 协程异步加载场景
        /// </summary>
        /// <param name="name"></param>
        /// <param name="fun"></param>
        /// <returns></returns>
        private IEnumerator ReallyLoadSceneAsyn(string name, Action fun)
        {
            AsyncOperation ao = SceneManager.LoadSceneAsync(name);
            //可以得到场景加载的一个进度
            while (!ao.isDone)
            {
                //事件中心 向外分发 进度情况  外面想用就用
                DSEntity.MsgMechain.Sender("进度条更新", ao.progress);
                //这里面去更新进度条
                yield return ao.progress;
            }

            //加载完成过后 才会去执行fun
            fun();
        }

        public override void ShutDown()
        {
        }
    }
}